<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class BillModel extends Model
{
    //定义一个表名
    protected $table = 'bill';
    //定义主键
    protected $primaryKey = 'id';
	protected $guarded = [];

    //获取用户的发票
	static function getList($uid){
		$data = [];
		$list = BillModel::where('uid',$uid)->orderBy('is_default','asc')->get();
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$data[$k]['name'] = $v['name'];
			$data[$k]['is_default'] = $v['is_default'];
			$data[$k]['type'] = $v['type'];
			if($v['type'] == 1){
				$data[$k]['typename'] = '个人';
			}else{
				$data[$k]['typename'] = '公司';
			}
		}

		return $data;
	}

	//添加用户发票
	static function insert($type,$uid,$name,$is_default,$duty='',$address='',$phone='',$bank='',$bank_num=''){
		BillModel::create([
			'type' => $type,
			'name' => $name,
			'uid' => $uid,
			'duty' => $duty,
			'address' => $address,
			'phone' => $phone,
			'bank' => $bank,
			'bank_num' => $bank_num,
			'is_default' => $is_default,
		]);
	}

	//删除发票
	static function del($id){
		if(BillModel::where('id',$id)->delete()){
			return 1;
		}else{
			return 2;
		}
	}

	//查询发票
	static function list($id){
		$list = BillModel::where('id',request()->id)->first();
		$data['bill']['id'] = $list['id'];
		$data['bill']['name'] = $list['name'];
		$data['bill']['duty'] = $list['duty'];
		$data['bill']['address'] = $list['address'];
		$data['bill']['phone'] = $list['phone'];
		$data['bill']['bank'] = $list['bank'];
		$data['bill']['bank_num'] = $list['bank_num'];
		$data['bill']['is_default'] = $list['is_default'];

		return $data;
	}

	//编辑发票
	static function upd_one($id, $name, $is_default, $duty='',$address = '', $phone = '', $bank = '', $bank_num = ''){
		BillModel::where('id',$id)->update([
			'name' => $name,
			'is_default' => $is_default,
			'duty' => $duty,
			'address' => $address,
			'phone' => $phone,
			'bank' => $bank,
			'bank_num' => $bank_num,
		]);
	}
}
